#region Copyright
//       Copyright (c) 2009 - Antoine Jaussoin - http://www.jaussoin.com/blog/
// 
//        This program is free software: you can redistribute it and/or modify
//        it under the terms of the GNU General Public License as published by
//        the Free Software Foundation, either version 3 of the License, or
//        (at your option) any later version.
// 
//        This program is distributed in the hope that it will be useful,
//        but WITHOUT ANY WARRANTY; without even the implied warranty of
//        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//        GNU General Public License for more details.
// 
//        You should have received a copy of the GNU General Public License
//        along with this program.  If not, see <http://www.gnu.org/licenses/>.
#endregion
using System.Collections;
using System.Collections.Generic;
using FT.Architecture.Example.Core.Repositories;
using FT.Architecture.Example.Core.Entities.Pets;
using FT.Architecture.Logging;
using FT.Architecture.NHibernate.Repositories;
using FT.Architecture.NHibernate.SessionManagement;
using FT.Architecture.Queries;
using NHibernate.Criterion;

namespace FT.Architecture.Example.Data.Repositories
{
    /// <summary>
    /// <see cref="NHibernate"/> implementation of <see cref="IPetRepository"/>
    /// </summary>
    public class NHibernatePetRepository : NHibernateRepository<IPet>, IPetRepository 
    {
        public NHibernatePetRepository(ISessionProviderService sessionProvider, IQueryExecutorService queryExecutor, ILogService logService) : base(sessionProvider, queryExecutor, logService)
        {
        }

        /// <summary>
        /// Used for benchmark purpose
        /// </summary>
        /// <returns></returns>
        public IList<PetView> GetPetViews()
        {
            IList results = SessionProvider.GetSession().CreateQuery("select p.Name, p.ID from IPet p")
                //.ApplySettings(_unitOfWork)
                .List();
            //todo: abstract the CreateQuery bit so it transparently comes from either a normal or a stateless session
            
            IList<PetView> views = new List<PetView>();

            foreach (IList rows in results)
            {
                views.Add(new PetView((string) rows[0],(long) rows[1]));
            }

            return views;
        }

        /// <summary>
        /// Used for benchmark purpose
        /// </summary>
        /// <returns></returns>
        public IList<PetView> GetPetViewsCriteria()
        {
            IList results = SessionProvider.GetSession().CreateCriteria(typeof(IPet))
                .SetProjection(Projections.Property("Name"), Projections.Property("ID"))
                .List();

            IList<PetView> views = new List<PetView>();

            foreach (IList rows in results)
            {
                views.Add(new PetView((string)rows[0], (long)rows[1]));
            }

            return views;
        }
    }
}
